import 'package:flutter/material.dart';
import 'package:flutter_jd/pages/tabbar/home/controller.dart';
import 'package:get/get_state_manager/get_state_manager.dart';

// 顶部背景
class Background extends GetWidget<HomeController> {
  @override
  Widget build(BuildContext context) {
    return _buildBackground();
  }

  // 构建背景（可使用默认背景、图片背景）
  _buildBackground([String type = 'default']) {
    if (type == 'default') {
      BoxDecoration defaultBackground = BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            Color(0xFFFB2E1B),
            Color(0xFFFF5640),
          ],
        ),
      );

      return Obx(
        () => Container(
          child: Column(
            children: [
              Container(
                height: 85,
                decoration: defaultBackground,
              ),
              Container(
                child: ClipPath(
                  clipper: BackgroundClipper(),
                  child: Container(
                    width: double.infinity,
                    height: controller.offsetTop.value <=
                            controller.backgroundHeightDynamic.value
                        ? (controller.backgroundHeightDynamic.value -
                            controller.offsetTop.value)
                        : 0,
                    decoration: defaultBackground,
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    } else if (type == 'image') {
      // BoxDecoration imageBackground = BoxDecoration(
      //   gradient: LinearGradient(
      //     begin: Alignment.topLeft,
      //     end: Alignment.topRight,
      //     colors: [
      //       Color(0xFFFE225D),
      //       Color(0xFFF62C1E),
      //     ],
      //   ),
      // );

      return Container(
        child: Column(
          children: [
            Container(
              height: 85,
            ),
            Container(
              child: ClipPath(
                clipper: BackgroundClipper(),
                child: Container(
                  width: double.infinity,
                  height: controller.offsetTop.value <=
                          controller.backgroundHeightDynamic.value
                      ? (controller.backgroundHeightDynamic.value -
                          controller.offsetTop.value)
                      : 0,
                  child: Image.asset(
                    'assets/images/my/my_bg.png',
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ),
          ],
        ),
      );
    }
  }
}

// 顶部背景圆弧
class BackgroundClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    Path path = Path();
    path.lineTo(0, size.height / 1.2);
    path.cubicTo(0, size.height, size.width * 1.2, size.height, size.width, 0);
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
